﻿function DrawMatched(item) {
    var html = '';
    item.forEach(function (data) {
        html += '<div class="history-row"> ' + DrawQueueItem(data);

        var QueueDetails = data.QueueDetails;
        if (data != null && data.ArrayValue != null) {
            var arrayValue = data.ArrayValue.split('^');
            var arrayQueue = [];
            var arrayQueueKL_Gia = [];
            var isFirst = true;
            var preDM = '', preDB = ''; //luu gia tri truoc do cua gia, de tinh cho truong hop DM_Gia1 hoac DB_Gia1 khong co
            for (var j = 0; j < arrayValue.length; j++) {
                var dataArray = null;
                if (arrayValue[j] != null)
                    dataArray = arrayValue[j].split('|');
                if (dataArray.length > 10) {
                    var DM_Gia1 = dataArray[6], KL_Gia = dataArray[8], KL_KL = dataArray[9];
                    var DB_Gia1 = dataArray[11];
                    var DB = '', DM = '';
                    if (DM_Gia1 != null && DM_Gia1 != '')
                        DM = DM_Gia1;                    
                    if (DB_Gia1 != null && DB_Gia1 != '')
                        DB = DB_Gia1;
                    if (DB == 'ATC' || DB == 'ATO' || DM == 'ATC' || DM == 'ATO') {


                    }
                    else if (KL_Gia != '' && KL_KL != '' && KL_KL.indexOf('+006') < 0) {
                        var date = (dataArray[0].split(' '))[1];                      
                        
                        if (isFirst == false) {
                            if (DM == '')
                                DM = preDM;
                            if (DB == '')
                                DB = preDB;
                        }
                        preDM = DM;
                        preDB = DB;
                        DM = parseFloat(DM);
                        DB = parseFloat(DB);                        
                        isFirst = false;
                        KL_Gia = parseFloat(KL_Gia);                        
                        arrayQueue.push([KL_Gia, DM, DB, KL_KL]);
                        if ($.inArray(KL_Gia, arrayQueueKL_Gia) < 0 && KL_Gia > 0)
                            arrayQueueKL_Gia.push(KL_Gia);
                    }
                }
            }
            var arrayMatchList = [];
            var maxVolume = 0;
            var arrayGroupSort = arrayQueueKL_Gia.sort(function (a, b) { return b - a });
            arrayGroupSort.forEach(function (arrayGroupItem) {
                var buyCount = 0, sellCount = 0, buyVolume = 0, sellVolume = 0;
                arrayQueue.forEach(function (item) {
                    if (item[0] == arrayGroupItem && item[2] != -1 && item[1] != -1) {
                        var KL_Gia = item[0], DM = item[1], DB = item[2];
                        if (KL_Gia == DB || KL_Gia > DM) //nhathn 06/11/2015
                        {
                            buyCount += 1;
                            buyVolume += parseInt(item[3]);
                        }
                        else {
                            sellCount += 1;
                            sellVolume += parseInt(item[3]);
                        }
                    }
                })
                maxVolume = buyVolume > maxVolume ? buyVolume : maxVolume;
                maxVolume = sellVolume > maxVolume ? sellVolume : maxVolume;
                arrayMatchList.push([buyCount, buyVolume, parseFloat(arrayGroupItem), sellVolume, sellCount]);
            });
            html += ' <div class="history-right">' + DrawMatchedItem(arrayMatchList, maxVolume, data.QueueDetails.GiaThamChieu) + '</div>';
        }
        html += '</div>';
    })
    $('.modalHistoryData .modal-body div').html(html);
}

function DrawMatchedItem(dataMatched, maxMatchedVolume, giaThamChieu) {
    
    var html = '';
    var htmlMatched = '';
    var totalBuy = 0, totalSell = 0, totalSVolume = 0, totalBVolume = 0;
    var htmlMatchedTitle = '<h6 class="titletheadc">' + lblMatchedBuySell + ' <span>CurrentMatchedTTVolume</span></h6>';
    var htmlMatchedHeader = '<div class="history-item">';
    htmlMatchedHeader += '<div class="item-col-1 color_yellow">BT</div>';
    htmlMatchedHeader += '<div class="item-col-2 color_yellow text-center">';
    htmlMatchedHeader += 'TotalBuyCurrentMatched';
    htmlMatchedHeader += '</div>';
    htmlMatchedHeader += '<div class="item-col-2 color_yellow">TotalBuyCurrentVolumeMatched</div>';
    htmlMatchedHeader += '<div class="item-col-1">' + lblBuy + '</div>';
    htmlMatchedHeader += '<div class="item-col-1">' + lblSell + '</div>';
    htmlMatchedHeader += '<div class="item-col-2 color_yellow text-right">TotalSellCurrentVolumeMatched</div>';
    htmlMatchedHeader += '<div class="item-col-2 color_yellow text-center">';
    htmlMatchedHeader += 'TotalSellCurrentMatched';
    htmlMatchedHeader += '</div>';
    htmlMatchedHeader += '<div class="item-col-1 color_yellow">ST</div>';
    htmlMatchedHeader += '</div>';
    for (var i = 0; i < dataMatched.length; i++) {
        var SCount = dataMatched[i][4];
        var BCount = dataMatched[i][0];
        var SVolume = dataMatched[i][3];
        var BVolume = dataMatched[i][1];
        var SPrice = dataMatched[i][2];
        var BPrice = dataMatched[i][2];
        
        totalSell += SCount;
        totalBuy += BCount;
        totalSVolume += SVolume;
        totalBVolume += BVolume;
        htmlMatched += ' <div class="history-item">';
        htmlMatched += '<div class="item-col-1">' + CheckNumberValue(BCount) + '</div>';
        htmlMatched += '<div class="item-col-2">';        
        htmlMatched += GetProgressingBarPopup(BVolume, maxMatchedVolume, true);
        htmlMatched += '</div>';
        htmlMatched += '<div class="item-col-2">' + DinhDang(BVolume) + '</div>';
        htmlMatched += '<div class="item-col-1 ' + SetColor(BPrice, giaThamChieu) + '">' + (CheckNumberValue(BPrice) == 0 ? "&nbsp" : CheckNumberValue(BPrice)) + '</div>';
        htmlMatched += '<div class="item-col-1 ' + SetColor(SPrice, giaThamChieu) + '">' + (CheckNumberValue(SPrice) == 0 ? "&nbsp" : CheckNumberValue(SPrice)) + '</div>';
        htmlMatched += '<div class="item-col-2 text-right">' + DinhDang(SVolume) + '</div>';
        htmlMatched += '<div class="item-col-2">';
        htmlMatched += GetProgressingBarPopup(SVolume, maxMatchedVolume, false);
        htmlMatched += '</div>';
        htmlMatched += '<div class="item-col-1">' + CheckNumberValue(SCount) + '</div>';
        htmlMatched += '</div>';
    }
    html += htmlMatchedTitle.replace('CurrentMatchedTTVolume', DinhDang(totalBVolume + totalSVolume));
    html += htmlMatchedHeader.replace('TotalBuyCurrentMatched', DinhDang(totalBuy)).replace('TotalSellCurrentMatched', DinhDang(totalSell)).replace('TotalBuyCurrentVolumeMatched', DinhDang(totalBVolume)).replace('TotalSellCurrentVolumeMatched', DinhDang(totalSVolume));
    html += htmlMatched;

    return html;
}

function DrawQueueItem(data) {
    var MaxQueue = data.MaxQueue;
    
    var date = data.GetTimeStr;
    data = data.QueueDetails;
    var DM_KL1 = parseInt(data.DM_KL1);
    var DM_KL2 = parseInt(data.DM_KL2);
    var DM_KL3 = parseInt(data.DM_KL3);
    var DM_Gia1 = parseFloat(data.DM_Gia1);
    DM_Gia1 = CheckPrice(DM_Gia1);
    var DM_Gia2 = parseFloat(data.DM_Gia2);
    DM_Gia2 = CheckPrice(DM_Gia2);
    var DM_Gia3 = parseFloat(data.DM_Gia3);
    DM_Gia3 = CheckPrice(DM_Gia3);
    var DB_KL1 = parseInt(data.DB_KL1);
    var DB_KL2 = parseInt(data.DB_KL2);
    var DB_KL3 = parseInt(data.DB_KL3);
    var DB_Gia1 = parseFloat(data.DB_Gia1);
    DB_Gia1 = CheckPrice(DB_Gia1);
    var DB_Gia2 = parseFloat(data.DB_Gia2);
    DB_Gia2 = CheckPrice(DB_Gia2);
    var DB_Gia3 = parseFloat(data.DB_Gia3);
    DB_Gia3 = CheckPrice(DB_Gia3);
    var GiaThamChieu = parseFloat(data.GiaThamChieu);
    var totalBuyVolume = 0;
    totalBuyVolume += DM_KL1.toString() == "NaN" ? 0 : DM_KL1;
    totalBuyVolume += DM_KL2.toString() == "NaN" ? 0 : DM_KL2;
    totalBuyVolume += DM_KL3.toString() == "NaN" ? 0 : DM_KL3;
    var totalSellVolume = 0;
    totalSellVolume += DB_KL1.toString() == "NaN" ? 0 : DB_KL1;
    totalSellVolume += DB_KL2.toString() == "NaN" ? 0 : DB_KL2;
    totalSellVolume += DB_KL3.toString() == "NaN" ? 0 : DB_KL3;

    var html = '';
    html += '    <div class="history-left">';
    html += '                       <h6 class="titletheadc"><span>' + date + '</span> ' + lblQueueBuySell + '</h6>';
    html += '                       <div class="history-item">';
    html += '                           <div class="item-col-2">';
    html += '                               &nbsp;';
    html += '    </div>';
    html += '    <div class="item-col-2 color_yellow">' + DinhDang(totalBuyVolume) + '</div>';
    html += '    <div class="item-col-1 ">'+lblBuy+'</div>';
    html += '    <div class="item-col-1">'+lblSell+'</div>';
    html += '    <div class="item-col-2 color_yellow text-right">' + DinhDang(totalSellVolume) + '</div>';
    html += '    <div class="item-col-2">';
    html += '        &nbsp;';
    html += '    </div>';
    html += '</div>';
    html += '<div class="history-item">';
    html += '    <div class="item-col-2">';
    html += GetProgressingBarPopup(DM_KL1, MaxQueue, true);
    html += '    </div>';
    html += '    <div class="item-col-2">' + DinhDang(DM_KL1) + '</div>';
    html += '    <div class="item-col-1 ' + SetColor(DM_Gia1, GiaThamChieu) + '">' + DM_Gia1 + '</div>';
    html += '    <div class="item-col-1 ' + SetColor(DB_Gia1, GiaThamChieu) + '">' + DB_Gia1 + '</div>';
    html += '    <div class="item-col-2  text-right">' + DinhDang(DB_KL1) + '</div>';
    html += '    <div class="item-col-2">';
    html += GetProgressingBarPopup(DB_KL1, MaxQueue, false);
    html += '    </div>';
    html += '</div>';
    html += '<div class="history-item">';
    html += '    <div class="item-col-2">';
    html += GetProgressingBarPopup(DM_KL2, MaxQueue, true);
    html += '    </div>';
    html += '    <div class="item-col-2">' + DinhDang(DM_KL2) + '</div>';
    html += '    <div class="item-col-1 ' + SetColor(DM_Gia2, GiaThamChieu) + '">' + DM_Gia2 + '</div>';
    html += '    <div class="item-col-1 ' + SetColor(DB_Gia2, GiaThamChieu) + '">' + DB_Gia2 + '</div>';
    html += '    <div class="item-col-2  text-right">' + DinhDang(DB_KL2) + '</div>';
    html += '    <div class="item-col-2">';
    html += GetProgressingBarPopup(DB_KL2, MaxQueue, false);
    html += '    </div>';
    html += '</div>';
    html += '<div class="history-item">';
    html += '    <div class="item-col-2">';
    html += GetProgressingBarPopup(DM_KL3, MaxQueue, true);
    html += '    </div>';
    html += '    <div class="item-col-2">' + DinhDang(DM_KL3) + '</div>';
    html += '    <div class="item-col-1 ' + SetColor(DM_Gia3, GiaThamChieu) + '">' + DM_Gia3 + '</div>';
    html += '    <div class="item-col-1 ' + SetColor(DB_Gia3, GiaThamChieu) + '">' + DB_Gia3 + '</div>';
    html += '    <div class="item-col-2  text-right">' + DinhDang(DB_KL3) + '</div>';
    html += '    <div class="item-col-2">';
    html += GetProgressingBarPopup(DB_KL3, MaxQueue, false);
    html += '    </div>';
    html += '</div>';
    html += '</div>';    
    return html;
}

function GetProgressingBarPopup(current, max, isRight) {
    var html = '';
    current = current != null ? parseInt(current) : 0;
    max = max != null ? parseInt(max) : 0;
    var progress = 0;
    if (max != 0)
        progress = current / max;
    progress = progress * 100;
    progress = progress > 0 && progress <= 1 ? 1 : progress;
    html += '<div class="progress progress-stock" style="width: 100%">';
    html += '<div class="progress-bar progress-bar-warning ' + (isRight == true ? " pull-right" : " pull-left") + '" style="width: ' + progress + '%"></div>';
    html += '</div>';
    return html;
}

function addPeriod(nStr) {
    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function ConvertShortPrice(price) {
    if (price < 1000000) {
        var number = price / 1000;
        return (addPeriod(number.toFixed(2)) + lblThousand);
    }
    else if (price < 1000000000) {
        var number = price / 1000000;
        return (addPeriod(number.toFixed(2)) + lblMillion);
    }
    else {
        var number = price / 1000000000;
        return (addPeriod(number.toFixed(2)) + lblBillion);
    }
}